package com.bw.a2212a_smartpark

import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.a2212a_smartpark.databinding.ActivitySplashBinding
import com.bw.base.path.RouterPath
import com.bw.common.adapter.ImageBannerAdapter
import com.bw.common.entity.BannerEntity
import com.bw.common.view.BaseMvvmActivity
import com.bw.image.imageUtils.glide.GlideImgLoader
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer
import dagger.hilt.android.AndroidEntryPoint

/**
 * 作者：
 * 日期：
 * 描述：
 * 业务：1.倒计时  2.APP主图banner切换   3.广告
 */
@AndroidEntryPoint
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {

    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter:ImageBannerAdapter
    private var mCountDownTimer : CountDownTimer? = null
    override fun getLayoutId(): Int = R.layout.activity_splash

    override fun initViews() {
        val isFirst = MMKV.defaultMMKV().decodeBool("isFirstBoot",true)
        if (isFirst) {
            MMKV.defaultMMKV().encode("isFirstBoot",false)
            showAppBanner()
        }else{
            showAppAdv()
        }


    }

    /**
     * App主图
     */
    private fun showAppBanner(){
        //View.VISIBLE表示显示UI  View.INVISIBLE表示不显示uI 但是可以占据UI大小和位置  View.Gone表示不显示UI 也不占据大小和位置
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.GONE

        bannerData.run{
            add(BannerEntity(0, com.bw.resource.R.drawable.wellcome_0,""))
            add(BannerEntity(1, com.bw.resource.R.drawable.wellcome_1,""))
            add(BannerEntity(2, com.bw.resource.R.drawable.wellcome_2,""))
        }

        mBannerAdapter = ImageBannerAdapter(bannerData)

        mDataBinding.appBanner.run{
            setAdapter(mBannerAdapter)
            //关联生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听
            addOnPageChangeListener(object:OnPageChangeListener{
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

                override fun onPageSelected(position: Int) {
                    //判断banner是否滑动到最后一个
                    if (position == bannerData.size-1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {}

            })
        }

        //点击立即进入
        mDataBinding.tvStart.setOnClickListener {
            this@SplashActivity.jumpMainPage()
            this@SplashActivity.finish()
        }
    }

    /**
     * APP广告
     */
    private fun showAppAdv(){
        mDataBinding.clAppBanner.visibility = View.GONE
        mDataBinding.clAppAdv.visibility = View.VISIBLE

        //显示广告图片
        mDataBinding.ivAdv.run {
            GlideImgLoader.loadResImage(this, R.drawable.img,this)
        }

        //5秒倒计时
        mCountDownTimer = object  :CountDownTimer(5000,1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text = "${millisUntilFinished/1000}  s 跳过"
            }
            override fun onFinish() {
                this@SplashActivity.jumpMainPage()
            }
        }

        //启动倒计时
        mCountDownTimer?.start()

        //点击倒计时跳过
        mDataBinding.tvAdvSkip.setOnClickListener {
            finish()
            this@SplashActivity.jumpMainPage()
        }

    }

    

    override fun onDestroy() {
        super.onDestroy()
        mCountDownTimer?.cancel()
    }





    private fun jumpMainPage(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
        this@SplashActivity.finish()
    }
}